from django.db.models import Model

from django.db import models
class Role(Model):
    name = models.CharField(max_length=50, unique=True, verbose_name="角色名称")  # 角色名称，唯一

    def __str__(self):
        return self.name


class User(Model):
    STUDENT = 1
    TEACHER = 2
    LEADER = 3
    ADMIN = 4
    ROLE_CHOICES = [
        (STUDENT, 'Student'),
        (TEACHER, 'Teacher'),
        (LEADER, 'Leader'),
        (ADMIN, 'Admin'),
    ]

    id = models.AutoField(primary_key=True, verbose_name="用户ID")  # 用户ID，自动递增
    username = models.CharField(max_length=100, unique=True, verbose_name="用户名")  # 用户名，唯一且非空
    password = models.CharField(max_length=255, verbose_name="密码")  # 密码，非空
    email = models.EmailField(unique=True, verbose_name="电子邮箱")  # 电子邮箱，唯一且非空
    role = models.IntegerField(choices=ROLE_CHOICES, verbose_name="用户角色")  # 用户角色
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")  # 创建时间，自动生成
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")  # 更新时间，自动更新

    class Meta:
        db_table = 'user'  # 指定表名
        verbose_name = '用户名'  # 后台管理看到的中文
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.username


class Course(Model):
    id = models.AutoField(primary_key=True, verbose_name="课程ID")  # 课程ID，自动递增
    name = models.CharField(max_length=100, verbose_name="课程名称")  # 课程名称，非空
    teacher = models.ForeignKey(User, on_delete=models.CASCADE, related_name="courses",
                                verbose_name="任课教师")  # 外键，关联教师

    class Meta:
        db_table = 'course'  # 指定表名
        verbose_name = '课程'  # 后台管理看到的中文
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.name


class TeacherEvaluation(Model):
    STUDENT_EVALUATION = 'student_evaluation'
    TEACHER_PEER_REVIEW = 'teacher_peer_review'
    LEADER_EVALUATION = 'leader_evaluation'
    EVALUATION_TYPE_CHOICES = [
        (STUDENT_EVALUATION, 'Student Evaluation'),
        (TEACHER_PEER_REVIEW, 'Teacher Peer Review'),
        (LEADER_EVALUATION, 'Leader Evaluation'),
    ]

    id = models.AutoField(primary_key=True, verbose_name="评价ID")  # 评价ID，自动递增
    evaluator = models.ForeignKey(User, on_delete=models.CASCADE, related_name="given_teacher_evaluations",
                                  verbose_name="评价者")  # 评价者
    evaluatee = models.ForeignKey(User, on_delete=models.CASCADE, related_name="received_teacher_evaluations",
                                  verbose_name="被评价者")  # 被评价者
    evaluation_type = models.CharField(max_length=50, choices=EVALUATION_TYPE_CHOICES, verbose_name="评价类型")  # 评价类型
    score = models.FloatField(verbose_name="评分")  # 评分
    feedback = models.TextField(null=True, blank=True, verbose_name="反馈意见")  # 反馈意见，可为空
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")  # 创建时间

    class Meta:
        db_table = 'teacher_evaluation'  # 指定表名
        verbose_name = '教师评价'  # 后台管理看到的中文
        verbose_name_plural = verbose_name

    def __str__(self):
        return f"{self.evaluator.username} -> {self.evaluatee.username} ({self.evaluation_type})"


class CourseEvaluation(Model):
    COURSE_EVALUATION = 'course_evaluation'
    EVALUATION_TYPE_CHOICES = [
        (COURSE_EVALUATION, 'Course Evaluation'),
    ]

    id = models.AutoField(primary_key=True, verbose_name="评价ID")  # 评价ID，自动递增
    evaluator = models.ForeignKey(User, on_delete=models.CASCADE, related_name="given_course_evaluations",
                                  verbose_name="评价者")  # 评价者
    course = models.ForeignKey(Course, on_delete=models.CASCADE, related_name="course_evaluations",
                               verbose_name="课程")  # 课程
    evaluation_type = models.CharField(max_length=50, choices=EVALUATION_TYPE_CHOICES, verbose_name="评价类型")  # 评价类型
    score = models.FloatField(verbose_name="评分")  # 评分
    feedback = models.TextField(null=True, blank=True, verbose_name="反馈意见")  # 反馈意见，可为空
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")  # 创建时间

    class Meta:
        db_table = 'course_evaluation'  # 指定表名
        verbose_name = '课程评价'  # 后台管理看到的中文
        verbose_name_plural = verbose_name

    def __str__(self):
        return f"{self.evaluator.username} -> {self.course.name} ({self.evaluation_type})"


class EvaluationCriteria(Model):
    STUDENT_EVALUATION = 'student_evaluation'
    TEACHER_PEER_REVIEW = 'teacher_peer_review'
    LEADER_EVALUATION = 'leader_evaluation'
    EVALUATION_TYPE_CHOICES = [
        (STUDENT_EVALUATION, 'Student Evaluation'),
        (TEACHER_PEER_REVIEW, 'Teacher Peer Review'),
        (LEADER_EVALUATION, 'Leader Evaluation'),
    ]

    id = models.AutoField(primary_key=True, verbose_name="评价指标ID")  # 评价指标ID，自动递增
    criteria_name = models.CharField(max_length=100, verbose_name="评价指标名称")  # 评价指标名称
    criteria_description = models.TextField(verbose_name="评价指标描述")  # 评价指标描述
    evaluation_type = models.CharField(max_length=50, choices=EVALUATION_TYPE_CHOICES, verbose_name="评价类型")  # 评价类型
    weight = models.FloatField(verbose_name="权重")  # 权重
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")  # 创建时间
    updated_at = models.DateTimeField(auto_now=True, verbose_name="更新时间")  # 更新时间

    class Meta:
        db_table = 'evaluation_criteria'  # 指定表名
        verbose_name = '评价指标'  # 后台管理看到的中文
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.criteria_name


class DataStatistics(Model):
    id = models.AutoField(primary_key=True, verbose_name="统计数据ID")  # 统计数据ID，自动递增
    course = models.ForeignKey(Course, on_delete=models.CASCADE, verbose_name="课程")  # 课程
    teacher = models.ForeignKey(User, on_delete=models.CASCADE, related_name="teaching_statistics",
                                verbose_name="教师")  # 教师
    average_score = models.FloatField(verbose_name="平均评分")  # 平均评分
    total_feedback_count = models.IntegerField(verbose_name="总反馈数")  # 总反馈数
    created_at = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")  # 创建时间

    class Meta:
        db_table = 'data_statistics'  # 指定表名
        verbose_name = '数据统计'  # 后台管理看到的中文
        verbose_name_plural = verbose_name

    def __str__(self):
        return f"Statistics for {self.course.name} by {self.teacher.username}"
